import java.io.*;
import java.util.Scanner;

public class text2 {
    //对普通文件的复制
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //输入复制路径
        System.out.println("要复制的文件");
        String path = sc.nextLine();

        File file = new File(path);

        if (file.isDirectory()) {
            System.out.println("不是文件");
            return;
        }

        String copyPath = null;
        //确认路径合法
        while(true){
            System.out.println("复制的路径");

            copyPath = sc.next();

            File f = new File(copyPath);
            if (f.isFile()) {
                System.out.println("路径不合法 重新输入");
            } else {
                break;
            }
        }

        //C:\Users\15328\OneDrive\桌面\files\4.javaee初阶课件\1. 计算机是如何工作的.pdf
        // C:\Users\15328\OneDrive\桌面
        //如果这个路劲不存在就创建他的父目录
        File Fold = new File(copyPath);

        System.out.println(Fold.mkdirs());
        //获得后缀getName会把后缀都给上
//        String suffix = path.substring(path.lastIndexOf("."));

        //复制文件 文件的内容也要复制
        try (InputStream inputStream = new FileInputStream(file)) {
            //套壳
            Scanner sc1 = new Scanner(inputStream);

            //复制
            File file2 = new File(copyPath +"/"+ file.getName());
            file2.createNewFile();

            OutputStream outputStream = new FileOutputStream(file2);

            PrintWriter write = new PrintWriter(outputStream);

            //读取整个文件
            while (sc1.hasNext()) {
                //读取
                String line = sc1.next();
                //写入
                write.println(line);
            }
            System.out.println("成功");
            outputStream.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
